/*
时间戳
*/

#pragma once

#include <stdio.h>
#include <algorithm>
#include <string>

class Timestamp
{
public:
    Timestamp():m_microSecondsSinceEpoch(0) {}

    explicit Timestamp(int64_t microSecondsSinceEpoch): m_microSecondsSinceEpoch(microSecondsSinceEpoch) {}

    //以下重载操作符

    Timestamp& operator+=(Timestamp lhs)
    {
        this->m_microSecondsSinceEpoch += lhs.m_microSecondsSinceEpoch;
        return *this;
    }

    Timestamp& operator+=(int64_t lhs)
    {
        this->m_microSecondsSinceEpoch +=lhs;
        return *this;
    }

    Timestamp& operator-=(Timestamp lhs)
    {
        this->m_microSecondsSinceEpoch -= lhs.m_microSecondsSinceEpoch;
        return *this;
    }

    Timestamp& operator-=(int64_t lhs)
    {
        this->m_microSecondsSinceEpoch -=lhs;
        return *this;
    }

    void swap(Timestamp& that)
    {
        std::swap(m_microSecondsSinceEpoch,that.m_microSecondsSinceEpoch);
    }


    const std::string toString() const;
    const std::string toFormattedString(bool showMicroSeconds = true) const;

    bool valid() const {return m_microSecondsSinceEpoch>0; }

    int64_t microSecondsSinceEpoch() const { return m_microSecondsSinceEpoch; }
    time_t secondsSinceEpoch()  const
    {
        return static_cast<time_t>(m_microSecondsSinceEpoch/kMicroSecondsPerSecond);
    }

    static Timestamp now();
    static Timestamp invalid();

    static const int kMicroSecondsPerSecond = 1000*1000;

private:
    //保存微妙
    int64_t m_microSecondsSinceEpoch;
};
